ADC INTERFACING WITH PIC
Analog-to-digital convertor (ADC) ICs are one way make the analog input compatible with the microcontroller. It is an intermediate device to convert the signals from analog to digital form.
Synopsis

A microcontroller, a digital device, can read, execute and transmit only digital signals. On the contrary, the outputs of the most of the transducers, temperature, pressure, force are analog in nature which is coverted into electrical signals. Thus it is hard to interface these transducers directly with controllers. Analog-to-digital convertor (ADC) ICs are one way make the analog input compatible with the microcontroller. It is an intermediate device to convert the signals from analog to digital form. These digital signals are used for further processing by the digital processors. Using an external ADC adds complexity to the circuit. To avoid this complexity, PIC Microcontrollers have in-built ADC module which reduces the cost and connections of the circuit. This article explains the in-built ADC of PIC18F4550 controller.

Description

A PIC microcontroller has inbuilt ADC for A/D conversion. The ADC module of PIC18F4550 controller has following specifications:

• 10-bit resolution output which means that an analog input gets converted into a corresponding 10-bit digital output.

• 13 channels which means that a total of 13 analog signals can be converted simultaneously into digital.

• Vref+ (RA3) and Vref- (RA2) pins for external reference voltage.

• 8 selectable clock options.

ADC can be in auto-triggering mode for continuous A/D conversion.

Specifications of ADCs

Most important specification of ADCs is the resolution. This specifies how accurately the ADC measures the analog input signals. Common ADCs are 8 bit, 10 bit and 12 bit. For example if the reference voltage ADC is 0 to 5v then a 8 bit ADC will break it in 256 divisions so it can measure it accurately up to 5/256 v= 19mV approx. While the 10 bit ADC will break the range in 5/1024 = 4.8mV approx. The ADC in PIC18 are 10 bit.

Other specification include the sampling rate, that means how fast the ADC can take readings. Microchip claims that pic18f4550’s ADC can go as high as 100K samples per second.

ADC Terminology

Reference Voltage: The reference voltage specifies the minimum and maximum voltage range of analog input. In PIC 18 there are two reference voltage, one is the Vref- and one is Vref+. The Vref- specifies the minimum input voltage of analog input while the Vref+ specifies the maximum. For example if the input signal Vref- is applied to analog input channel then the result of conversion will be 0 and if voltage equal to Vref+ is applied to the input channel the result will be 1023 (max value for 10bit ADC).

ADC Channels: The ADC module is connected to several channels via a multiplexer. The multiplexer can connect the input of the ADC to any of the available channels. This allows to connect many analog signals to the MCU (say 3 temperature sensors). In PIC18F4550 there are 13 analog input channels, they are named AN0, AN1 etc.

Acquisition Time: When an specific channel is selected the voltage from that input channel is stored in an internal holding capacitor. It takes some time for the capacitor to get fully charged and become equal to the applied voltage. This time is called acquisition time. The PIC18F4550’s ADC provides a programmable acquisition time, Once acquisition time is over the input channel is disconnected from the source and the conversion begin. The acquisition times depends on several factor like the source impedance, VDD of the system and temperature. You can refer to the page 227 and 228 in the datasheet for details on its calculation. A safe value is 2.45uS, so acquisition time must be set to any value more than this.

ADC Clock: ADC Requires a clock source to do its conversion, this is called ADC Clock. The time period of the ADC Clock is called TAD. It is also the time required to generate 1 bit of conversion. The ADC requires 11 TAD to do a 10 bit conversion. It can be derived from the CPU clock (called TOSC) by dividing it by a suitable division factor. There are Seven possible option.

• 2 x TOSC

• 4 x TOSC

• 8 x TOSC

• 16 x TOSC

• 32 x TOSC

• 64 x TOSC

• Internal RC

For Correct A/D Conversion, the A/D conversion clock (TAD) must be as short as possible but greater than the minimum TAD . See table 28-29 in PIC18F4550/PIC18F2550 datasheet). It is 0.7uS for PIC18FXXXX device and 1.4uS for PIC18LFXXXX device.

For example,

If we are running at 20MHz in our PIC Development board so we set prescaler of 32 TOSC.

Our FOSC = 20MHz

Therefore our FOSC = 1/20MHz

= 50nS

32 TOSC = 32 x 50 nS

= 1600nS

= 1.6uS

1.6 uS is more than the minimum requirement.

You can calculate the value for division factor using the above example in case you are using crystal of other frequency. Also now we have the TAD we can calculate the division factor for acquisition time. Acquisition time can be specified in terms of TAD. It can be set to one of the following values.

• 20 x TAD

• 16 x TAD

• 12 x TAD

• 8 x TAD

• 6 x TAD

• 4 x TAD

• 2 x TAD

• 0 x TAD

As we saw in above paragraph that the safe acquisition time is 2.45uS, so we select 2 x TAD as acquisition time.

TACQ=2 x TAD

=2 x 1.6uS (Replacing TAD= 1.6uS)

=3.2uS. 3.2uS is more than required 2.45uS.

ADC Registers:

To work with the inbuilt ADC of this PIC microcontroller, the certain registers are required to be configured. Each of these ADC registers has been explained below.

1. ADCON0 (A/D CONTROL REGISTER 0)


ADON: This bit is used to enable/disable the ADC peripheral of the PIC.

1 = A/D converter module is enabled

0 = A/D converter module is disabled

GO/DONE: This is A/D conversion status bit. For ADON=1,

1 = A/D conversion in progress

0 = A/D Idle

CHS3: CHS0: These bits are used to select a particular analog channel from 13 available channels (0-12) which are multiplexed with digital I/O pins. The following table shows the bit configuration to select these analog channels:


2. ADCON1 (A/D CONTROL REGISTER 1)


PCFG0:PCFG3: As mentioned earlier, there are 13 analog channels in PIC18F4550 which are multiplexed with digital I/O pins. This means that such a (multiplexed) pin can act as either a digital I/O pin or an analog input pin. Either of these configurations is selected by these bits. The following table shows the bit configuration to make a pin D (Digital I/O) or A (Analog input):


VCGF1-VCGF0: These are voltage configuration bits to select the reference voltage (Vref) for ADC module.


3. ADCON2 (A/D CONTROL REGISTER 2)


ADCS2:ADCS0: These bits are used to select the clock option for ADC peripheral. The following table shows the bit configuration to select from different clock options:


ACQT2:ACQT0: These bits are used to set the acquisition time of the ADC. The acquisition time is the time required to charge and discharge the holding capacitor of the ADC.

ADFM: This bit is used to select the format of digital output.

1 = Right justified (LSB to MSB)

0 = Left justified (MSB to LSB)

4. ADRESL & ADRESH:

Since the ADC module of PIC provides 10-bit digital output after A/D conversion, this output is stored in two 8-bit registers, namely, ADRESL & ADRESH. The lower byte is stored in ADRESL (A/D Result High register) while the higher byte is stored in ADRESH (A/D Result Low register).

Working with ADC

To select an analog channel of PIC18F4550’s in-built ADC and provide an analog input (0 to 5 volt) to it using a variable resistor or preset(20 k ). The main objective is to read the analog signal and display the corresponding digital value on LCD.

Programming Steps:

1. Set number of analog inputs by setting PCFG3:PCFG0 (ADCON1).

2. Select the analog channel by setting CHS3:CHS0 bits (ADCON0).

3. Select the clock option, acquisition time output format by configuring the ADCON2 register.

4. Enable the ADC by making ADON bit (ADCON0) high.

5. Start the conversion by setting GO/DONE bit (ADCON0).

6. Wait until GO/DONE bits becomes low. This indicates that the A/D conversion is over.

7. Store A/D conversion result from ADRESH:ADRESL into a variable.

8. Covert the resultant value to its corresponding ASCII value and display on LCD.

Proteus design for ADC interfacing with PIC


Orcad design for ADC interfacing with PIC


ADC interfacing with PIC

/*  Name     : ADCmain.c
 *  Purpose  : Source code for Inbuild ADC in PIC18F4550.
 *  Author   : Gemicates
 *  Date     : 2017-06-15
 *  Website  : www.gemicates.org
 *  Revision : None
 */

                                    // Program for Inbuild ADC in PIC18F4550
#include<htc.h>                     // Header file for PIC18F4550 series
#define _XTAL_FREQ 12000000         // 12MHz Crystal Frequency for PIC18F4550


void ADC_Init()
{
  ADCON0 = 0x41;                    // ADC Module Turned ON and Clock is selected
  ADCON1 = 0xC0;                    // All pins as Analog Input
}

unsigned int ADC_Read(unsigned char channel)
{
  if(channel > 7)                   // If Invalid channel selected 
    return 0;                       // Return 0
  
  ADCON0 &= 0xC5;                   // Clearing the Channel Selection Bits
  ADCON0 |= channel<<3;             // Setting the required Bits
  __delay_ms(2);                    // Acquisition time to charge hold capacitor
  GO_nDONE = 1;                     // Initializes A/D Conversion
  while(GO_nDONE);                  // Wait for A/D Conversion to complete
  return ((ADRESH<<8)+ADRESL);      // Returns Result
}

void main()                         // main function
{
  unsigned int a;
  TRISB = 0x00;                     // PORTB as output
  TRISC = 0x00;                     // PORTC as output
  TRISA = 0x10;                     // PORTA as input
  ADC_Init();                       // Initializes ADC Module

  do
  {
    a = ADC_Read(0);                // Reading Analog Channel 0
    PORTB = a;                      // Lower 8 bits to PORTB
    PORTC = a>>8;                   // Higher 2 bits to PORTC
    __delay_ms(60);                 // Delay
  }while(1);                        // Infinite Loop
}

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close